﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class ApportionRuleDA : IApportionRuleDA
    {
        private string ALLOW_OVER_SHIPMENT = "@ALLOW_OVER_SHIPMENT";
        private string CREATED_BY = "@CREATED_BY";
        private string CUSTOMER_ID = "@CUSTOMER_ID";
        private string MAXIMUM_PER_OVER = "@MAXIMUM_PER_OVER";
        private string OWNER_ID = "@OWNER_ID";
        private string SEQ_NO = "@SEQ_NO";
        private string SQL_CHECK_APPORTION_RULE_ID_UNIQUE = " SELECT COUNT(1) FROM @_@APPORTION_RULE WHERE OWNER_ID = @OWNER_ID AND CUSTOMER_ID = @CUSTOMER_ID AND SEQ_NO = @SEQ_NO ";
        private string SQL_DELETE_APPORTION_RULE = " DELETE FROM @_@APPORTION_RULE WHERE OWNER_ID = @OWNER_ID AND CUSTOMER_ID = @CUSTOMER_ID AND SEQ_NO = @SEQ_NO ";
        private string SQL_INSERT_APPORTION_RULE = " INSERT INTO @_@APPORTION_RULE ( WH_ID, OWNER_ID, CUSTOMER_ID, SEQ_NO, ALLOW_OVER_SHIPMENT, MAXIMUM_PER_OVER, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @OWNER_ID, @CUSTOMER_ID, @SEQ_NO, @ALLOW_OVER_SHIPMENT, @MAXIMUM_PER_OVER, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_APPORTION_RULE = " SELECT  ROWID, WH_ID, OWNER_ID, CUSTOMER_ID, SEQ_NO, ALLOW_OVER_SHIPMENT, MAXIMUM_PER_OVER, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@APPORTION_RULE ";
        private string SQL_UPDATE_APPORTION_RULE = " UPDATE @_@APPORTION_RULE SET WH_ID = @WH_ID, ALLOW_OVER_SHIPMENT = @ALLOW_OVER_SHIPMENT, MAXIMUM_PER_OVER = @MAXIMUM_PER_OVER, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE OWNER_ID = @OWNER_ID AND CUSTOMER_ID = @CUSTOMER_ID AND SEQ_NO = @SEQ_NO  ";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckApportionRuleIDUnique(string ownerID, string customerID, string seqNo, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@CUSTOMER_ID", customerID), new SqlParameter("@SEQ_NO", seqNo) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_APPORTION_RULE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckApportionRuleIDUnique(DataBase dataBase, DbTransaction tran, string ownerID, string customerID, string seqNo, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@CUSTOMER_ID", customerID), new SqlParameter("@SEQ_NO", seqNo) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_APPORTION_RULE_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteApportionRule(List<string> ownerID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string sqlTemp = this.SQL_DELETE_APPORTION_RULE + " OWNER_ID = @OWNER_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteApportionRule(List<string> ownerID, List<string> customerID, List<string> seqNo, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@CUSTOMER_ID", customerID[j]), new SqlParameter("@SEQ_NO", seqNo[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteApportionRule(DataBase dataBase, DbTransaction tran, List<string> ownerID, List<string> whLoginID)
        {
            int result = 0;
            string sqlTemp = this.SQL_DELETE_APPORTION_RULE + " OWNER_ID = @OWNER_ID";
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, sqlTemp.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteApportionRule(DataBase dataBase, DbTransaction tran, List<string> ownerID, List<string> customerID, List<string> seqNo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < ownerID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID[j]), new SqlParameter("@CUSTOMER_ID", customerID[j]), new SqlParameter("@SEQ_NO", seqNo[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllApportionRule(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllApportionRule(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public ApportionRuleInfo GetApportionRuleByID(string ownerID, string customerID, string seqNo, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND CUSTOMER_ID = @CUSTOMER_ID AND SEQ_NO = @SEQ_NO  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@CUSTOMER_ID", customerID), new SqlParameter("@SEQ_NO", seqNo) };
            ApportionRuleInfo apportionRuleInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    apportionRuleInfo = new ApportionRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["CUSTOMER_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["ALLOW_OVER_SHIPMENT"].ToString(), (reader["MAXIMUM_PER_OVER"].ToString() != "") ? decimal.Parse(reader["MAXIMUM_PER_OVER"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return apportionRuleInfo;
        }

        public ApportionRuleInfo GetApportionRuleByID(DataBase dataBase, DbTransaction tran, string ownerID, string customerID, string seqNo, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID AND CUSTOMER_ID = @CUSTOMER_ID AND SEQ_NO = @SEQ_NO  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID), new SqlParameter("@CUSTOMER_ID", customerID), new SqlParameter("@SEQ_NO", seqNo) };
            ApportionRuleInfo apportionRuleInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                apportionRuleInfo = new ApportionRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["CUSTOMER_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["ALLOW_OVER_SHIPMENT"].ToString(), (reader["MAXIMUM_PER_OVER"].ToString() != "") ? decimal.Parse(reader["MAXIMUM_PER_OVER"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return apportionRuleInfo;
        }

        public DataSet GetApportionRuleByQueryList(List<string> sqlWhere, ApportionRuleQueryEntity apportionruleQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!apportionruleQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, apportionruleQuery.CurrentPage, apportionruleQuery.PageSize, apportionruleQuery.SortField, apportionruleQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetApportionRuleByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, ApportionRuleQueryEntity apportionruleQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, apportionruleQuery.CurrentPage, apportionruleQuery.PageSize, apportionruleQuery.SortField, apportionruleQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<ApportionRuleInfo> GetApportionRuleListByID(string ownerID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID) };
            List<ApportionRuleInfo> apportionRuleInfos = new List<ApportionRuleInfo>();
            ApportionRuleInfo apportionRuleInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    apportionRuleInfo = new ApportionRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["CUSTOMER_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["ALLOW_OVER_SHIPMENT"].ToString(), (reader["MAXIMUM_PER_OVER"].ToString() != "") ? decimal.Parse(reader["MAXIMUM_PER_OVER"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    apportionRuleInfos.Add(apportionRuleInfo);
                }
            }
            return apportionRuleInfos;
        }

        public List<ApportionRuleInfo> GetApportionRuleListByID(DataBase dataBase, DbTransaction tran, string ownerID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_APPORTION_RULE.Replace("@_@", whLoginID) + " WHERE OWNER_ID = @OWNER_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@OWNER_ID", ownerID) };
            List<ApportionRuleInfo> apportionRuleInfos = new List<ApportionRuleInfo>();
            ApportionRuleInfo apportionRuleInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            while (reader.Read())
            {
                DateTime CS00002;
                apportionRuleInfo = new ApportionRuleInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["OWNER_ID"].ToString(), reader["CUSTOMER_ID"].ToString(), (reader["SEQ_NO"].ToString() != "") ? int.Parse(reader["SEQ_NO"].ToString()) : 0, reader["ALLOW_OVER_SHIPMENT"].ToString(), (reader["MAXIMUM_PER_OVER"].ToString() != "") ? decimal.Parse(reader["MAXIMUM_PER_OVER"].ToString()) : 0M, reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                apportionRuleInfos.Add(apportionRuleInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return apportionRuleInfos;
        }

        public int InsertApportionRule(ApportionRuleInfo apportionRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    apportionRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_ApportionRule_Parameters(apportionRuleInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertApportionRule(DataBase dataBase, DbTransaction tran, ApportionRuleInfo apportionRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    apportionRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_ApportionRule_Parameters(apportionRuleInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_ApportionRule_Parameters(ApportionRuleInfo apportionRuleInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.CUSTOMER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SEQ_NO, SqlDbType.Int, 4), new SqlParameter(this.ALLOW_OVER_SHIPMENT, SqlDbType.VarChar, 1), new SqlParameter(this.MAXIMUM_PER_OVER, SqlDbType.Decimal, 9), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(apportionRuleInfo.WhID))
            {
                paramArray[0].Value = apportionRuleInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = apportionRuleInfo.OwnerID;
            paramArray[2].Value = apportionRuleInfo.CustomerID;
            paramArray[3].Value = apportionRuleInfo.SeqNo;
            paramArray[4].Value = apportionRuleInfo.AllowOverShipment;
            paramArray[5].Value = apportionRuleInfo.MaximumPerOver;
            paramArray[6].Value = apportionRuleInfo.CreatedBy;
            paramArray[7].Value = apportionRuleInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateApportionRule(ApportionRuleInfo apportionRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    apportionRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_ApportionRule_Parameters(apportionRuleInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateApportionRule(DataBase dataBase, DbTransaction tran, ApportionRuleInfo apportionRuleInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    apportionRuleInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_ApportionRule_Parameters(apportionRuleInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_APPORTION_RULE.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

